package com.wnx.job.modules.ums.controller;

import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.PageResult;
import com.wnx.job.common.api.QueryPageBean;
import com.wnx.job.common.api.ResultData;
import com.wnx.job.common.mapstruct.DictMapper;
import com.wnx.job.domain.CascadeNode;
import com.wnx.job.modules.ums.dto.DictParam;
import com.wnx.job.modules.ums.model.UmsDict;
import com.wnx.job.modules.ums.service.UmsDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Slf4j
@RestController
@RequestMapping("/ums/umsDict")
@Api(tags = "DictControllerByStudent",description = "字典管理管理")
public class UmsDictController {

    @Resource
    private UmsDictService dictService;

    /**
     * 新增字典
     * @param dictParam
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增字典")
    public ResultData<String> save(@Validated @RequestBody DictParam dictParam){
        log.info("【=======系统正在增加字典中】================");
        UmsDict tbDict = DictMapper.INSTANCE.toPojo(dictParam);
        dictService.save(tbDict);
        log.info("【==========系统新增字典成功！=============】");
        return ResultData.success(Const.ADD_DICT_SUCCESS);
    }

    /**
     * 删除字典
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除字典")
    public ResultData<String> delete(@PathVariable(name = "id")Long id){
        log.info("【=========系统正在删除字典，删除字典ID为：{}=======】",id);
        dictService.removeById(id);
        log.info("【============系统删除字典成功！！===========】");
        return ResultData.success(Const.DELETE_DICT_SUCCESS);

    }


    /**
     * 修改字典
     * @param dictParam
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改字典")
    public ResultData<String> update(@Validated @RequestBody DictParam dictParam){
        log.info("【=======系统正在修改字典中，该字典的信息为：============】");
        UmsDict tbDict = DictMapper.INSTANCE.toPojo(dictParam);
        dictService.updateById(tbDict);
        log.info("【============系统修改字典成功！！===========】");
        return ResultData.success(Const.UPDATE_DICT_SUCCESS);
    }

    /**
     * 根据ID查询字典
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("根据ID查询字典")
    public ResultData<UmsDict> findById(@PathVariable(name = "id")Long id){
        log.info("【=======系统正在根据ID查询字典中....============】");
        UmsDict dict =dictService.getById(id);
        log.info("【=======系统正在根据ID查询字典成功,该字典的信息============】");
        return ResultData.success(Const.QUERY_DICT_SUCCESS,dict);
    }

    /**
     *  分页查询字典
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findByPage")
    @ApiOperation("分页查询字典")
    public ResultData<PageResult<UmsDict>> findByPage(@RequestBody QueryPageBean queryPageBean){
        log.info("【========系统正在分页查询字典,查询的条件为：{}========】",queryPageBean);
        PageResult<UmsDict> pageResult =  dictService.findByPage(queryPageBean);
        log.info("【========系统正在分页查询字典成功!========】");
        return ResultData.success(Const.QUERY_DICT_SUCCESS,pageResult);

    }

    /**
     * 获取城市树
     * @return
     */
    @GetMapping("/getCityTree")
    @ApiOperation("获取城市树")
    public ResultData<List<CascadeNode>> getCityTree(){
        log.info("=======系统正在获取城市树======");
        List<CascadeNode> cityTree = dictService.getCityTree();
        log.info("=======系统成功获取城市树！======");
        return ResultData.success(Const.QUERY_CITY_TYPE_TREE_SUCCESS,cityTree);

    }

    /**
     * 根据字典类型ID 查询对应字典集合
     * @param id
     * @return
     */
    @GetMapping("/findListByTypeId/{id}")
    @ApiOperation("（网站前台）根据字典类型ID 查询对应字典集合")
    public ResultData<List<UmsDict>> findDictListByTypeId(@PathVariable(name = "id") Long id){
        log.info("（网站前台）系统正在处理根据字典类型ID 查询对应字典集合！");
        //根据字典类型ID 查询对应字典集合
        List<UmsDict> listByTypeId = dictService.findDictListByTypeId(id);
        log.info("（网站前台）系统正在处理处理根据字典类型ID 查询对应字典集合！成功");
        return ResultData.success(Const.QUERY_DICT_SUCCESS,listByTypeId);

    }


}

